package kaituozhe.com.loadcontactdemo;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.ContactsContract;

import java.io.InputStream;
import java.util.ArrayList;

public class ContactUtils {

    /**
     * 获得所有的联系人
     *
     * @param context
     * @return
     */
    public static ArrayList<ContactBean> getAllContacts(Context context) {

        ArrayList<ContactBean> list = new ArrayList<ContactBean>();

        ContentResolver resolver = context.getContentResolver();

        Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        String[] projection = new String[]{
                ContactsContract.CommonDataKinds.Phone.NUMBER,// 电话号码
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,// 用户名
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID // 联系人id
        };
        String selection = null;
        String[] selectionArgs = null;
        String sortOrder = null;
        Cursor cursor = resolver.query(uri, projection, selection,
                selectionArgs, sortOrder);

        if (cursor != null) {
            while (cursor.moveToNext()) {
                String number = cursor.getString(0);// 电话号码
                String name = cursor.getString(1);
                long id = cursor.getLong(2);

                ContactBean object = new ContactBean();
                object.setName(name);
                object.setNumber(number);
                object.setContactId(id);
//                object.name = name;
//                object.number = number;
//                object.contactId = id;
                list.add(object);
            }
            cursor.close();
        }
        return list;
    }

    /**
     * 获得联系人的图片
     *
     * @param context
     * @param contactId
     * @return
     */
    public static Bitmap getContactIcon(Context context, long contactId) {
        ContentResolver resolver = context.getContentResolver();
        Uri contactUri = Uri.withAppendedPath(
                ContactsContract.Contacts.CONTENT_URI, contactId + "");
        InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(
                resolver, contactUri);

        // 将流转化bitmap
        return BitmapFactory.decodeStream(is);
    }

    /**
     * 获得游标集合
     *
     * @param context
     * @return
     */
    public static Cursor getAllCursor(Context context) {
        ContentResolver resolver = context.getContentResolver();

        Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        String[] projection = new String[]{
                ContactsContract.CommonDataKinds.Phone._ID,
                ContactsContract.CommonDataKinds.Phone.NUMBER,// 电话号码
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,// 用户名
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID // 联系人id
        };
        String selection = null;
        String[] selectionArgs = null;
        String sortOrder = null;
        return resolver.query(uri, projection, selection, selectionArgs,
                sortOrder);
    }

    public static ContactBean getContactBean(Cursor cursor) {

        ContactBean bean = new ContactBean();
        bean.setName(cursor.getString(1));
        bean.setNumber(cursor.getString(2));
        bean.setContactId(cursor.getLong(3));
//        bean.number = cursor.getString(1);
//        bean.name = cursor.getString(2);
//        bean.contactId = cursor.getLong(3);

        return bean;
    }
}
